import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

/*
 * Author: Anthony Fleshner
 * 
 * Use actionPerformed "if" statements to update the Link positions in "robot".
 */

class ButtonListener implements ActionListener {

	RobotPanel robot;
	ControlPanel control;
	SimThread robotSim;
	
	MouseClick click;
	//RobotMouseClick robot_click;
	
	//Add Robot Popup
    GridLayout grid;
	JPanel robot_contentPane;
    JTextField initialX;
    JTextField initialY;
    JTextField k1Matrix;
    JTextField k2Matrix;
    JTextField k3Matrix;
    JTextField k4Matrix;
    JButton robot_done;
    JPopupMenu addRobotPanel;
    
    //Add Light Menu
    JPopupMenu addLightPanel;
    JPanel light_contentPane;
    JTextField intensity;
    
    
	public ButtonListener(RobotPanel rob, ControlPanel con)
	{
		robot = rob;
		control = con;
		
		click = new MouseClick(robot, control, this);
		
		//Create Add Robot PopUp
	      grid = new GridLayout(0,1);
		  robot_contentPane = new JPanel(grid);
	      initialX = new JTextField();
	      initialY = new JTextField();
	      k1Matrix = new JTextField();
	      k2Matrix = new JTextField();
	      k3Matrix = new JTextField();
	      k4Matrix = new JTextField();
	    
	      addRobotPanel = new JPopupMenu();
	     
	      robot_contentPane.add(new JLabel("K Matrix:"));
	      robot_contentPane.add(k1Matrix);
	      robot_contentPane.add(k2Matrix);
	      robot_contentPane.add(k3Matrix);
	      robot_contentPane.add(k4Matrix);
	     

	      robot_contentPane.setVisible(false);
	      control.add(robot_contentPane);

	      //Create Add Light PopuP
	      intensity = new JTextField();
	      addLightPanel = new JPopupMenu();
	      light_contentPane = new JPanel(new GridLayout(0,1));
	      light_contentPane.add(new JLabel("Intensity: "));
	      light_contentPane.add(intensity);
	      light_contentPane.add(new JLabel(" "));
	      light_contentPane.add(new JLabel(" "));
	      light_contentPane.setVisible(false);
	      control.add(light_contentPane);
	}
	
  public JTextField getInitialX() {
		return initialX;
	}

	public JTextField getInitialY() {
		return initialY;
	}

	public JTextField getK1Matrix() {
		return k1Matrix;
	}

	public JTextField getK2Matrix() {
		return k2Matrix;
	}

	public JTextField getK3Matrix() {
		return k3Matrix;
	}

	public JTextField getK4Matrix() {
		return k4Matrix;
	}
	public JTextField getIntesity(){
		return intensity;
	}
	
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Add Robot")) {
      System.out.println("Added a Robot to the Simulation");
      //show pop up menu asking for K matrix, initial position, and size
      
      if(control.add_robot == false){

    	  robot_contentPane.setVisible(true);
    	  control.add_robot = true;
    	  
          
          if(control.add_light){
        	  control.addLightToggle.doClick();
          }
    	  
          robot.addMouseListener(click);
          control.repaint();
          
      }
      else
      {
    	  control.add_robot = false;
    	  robot_contentPane.setVisible(false);
      }
      

    }
    else if (e.getActionCommand().equals("Add Light")) {
        System.out.println("Added a Light to the Simulation");
        //prompt user to click on the screen
        if(control.add_light == false){
    		control.add_light = true;
    		light_contentPane.setVisible(true);
    		
        	if(control.add_robot)
        	{
        		control.addRobotToggle.doClick();
        	}
            
            robot.addMouseListener(click);
            control.repaint();
        }
        else
        {
        	control.add_light = false;
      	    light_contentPane.setVisible(false);
        }
        

      }
    else if (e.getActionCommand().equals("Start!")) {
    	control.startSim.setText("Stop!");
    	robotSim = new SimThread(robot);
    	robotSim.start();
    	// code to start the simulation
    }
    else if (e.getActionCommand().equals("Stop!")) {
    	control.startSim.setText("Start!");
    	robotSim.killThread();
    	// code to pause the simulation
    }
    else if(e.getActionCommand().equals("Clear"))
    {
    	robot.clear();
    }
  }
}
